load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("//third_party:substitution.bzl", "header_template_rule")
load("//tools/config:defs.bzl", "if_cuda")

header_template_rule(
    name = "cmake_macros_h",
    src = "macros/cmake_macros.h.in",
    out = "macros/cmake_macros.h",
    substitutions = {
        "cmakedefine": "define",
        "#define C10_USE_NUMA": "/* #undef C10_USE_NUMA */",
    },
)

header_template_rule(
    name = "cuda_cmake_macros_h",
    src = "cuda/impl/cuda_cmake_macros.h.in",
    out = "cuda/impl/cuda_cmake_macros.h",
    substitutions = {
        "cmakedefine": "define",
    },
)

cc_library(
    name = "headers",
    hdrs = glob([
        "core/*.h",
        "core/impl/*.h",
        "cuda/*.h",
        "cuda/impl/*.h",
        "macros/*.h",
        "mobile/*.h",
        "util/*.h",
        "util/*.hpp",
    ]),
    deps = [
        "@com_github_gflags_gflags//:gflags",
        "@com_github_glog//:glog",
        ":cmake_macros_h",
        ":cuda_cmake_macros_h",
    ],
    visibility = ["//:__pkg__"],
)

cc_library(
    name = "c10",
    srcs = glob([
        "core/*.cpp",
        "core/impl/*.cpp",
        "mobile/*.cpp",
        "util/*.cpp",
    ]) + if_cuda(
        glob([
            "cuda/*.cpp",
            "cuda/impl/*.cpp",
        ]),
        [],
    ),
    copts = ["-DCAFFE2_BUILD_MAIN_LIB"],
    deps = [
        ":headers",
        "@fmt",
    ] + if_cuda(
        ["@cuda"],
        [],
    ),
    alwayslink = True,
    visibility = ["//:__pkg__"],
)

cc_test(
    name = "tests",
    size = "small",
    srcs = glob([
        "test/util/*.cpp",
        "test/util/*.h",
        "test/core/*.cpp",
        "test/core/impl/*.cpp",
    ]),
    copts = ["-Wno-deprecated-declarations"],
    deps = [
        ":c10",
        ":headers",
        "@com_google_googletest//:gtest_main",
    ],
    visibility = ["//:__pkg__"],
)
